package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.pojo.OrderSettings;
import cn.itcast.service.OrderSettingService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Reference;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @ClassName OrderSettingController
 * @Description TODO
 * @Author lc
 * @Date 2021/11/15 16:15
 * @Version 1.0
 */
@RestController
@RequestMapping(path = "/ordersetting")
@Slf4j
public class OrderSettingController {
    @Reference(check = false)
    private OrderSettingService orderSettingService;

    /**
     * 查询当前年月的预约数据
     *
     * @param currentYearAndMonth
     * @return
     */
    @GetMapping("/getOrderSettingsByMonth")
    public Result getOrderSettingsByMonth(@RequestParam("date") String currentYearAndMonth) {
        try {
            log.info("查询当前年月的预约数据 data:{}",currentYearAndMonth);
            List<OrderSettings> orderSettingsByMonth = orderSettingService.getOrderSettingsByMonth(currentYearAndMonth);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, orderSettingsByMonth);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 新增或者修改可预约人数,修改人数时不可少于已预约人数
     *
     * @param orderSetting
     * @return
     */
    @PutMapping("/updateOrderSettingNumber")
    public Result updateOrderSettingNumber(@RequestBody OrderSetting orderSetting) {
        try {
            log.info("Controller 修改预约人数 OrderSetting:{}",orderSetting);
            Boolean isSucceed = orderSettingService.updateOrderSettingNumber(orderSetting);
            if (isSucceed) {
                return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
            } else {
                return new Result(false, MessageConstant.ORDERSETTING_FAIL);
            }
        } catch (Exception e) {
            log.error("修改预约人数异常e={}",e);
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    /**
     * excel批量导入数据
     *
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public Result uploadFile(@RequestParam("excelFile") MultipartFile multipartFile) throws IOException {
        try {
            Workbook sheets = WorkbookFactory.create(multipartFile.getInputStream());
            //取得excel的第一张表
            Sheet sheetAt = sheets.getSheetAt(0);
            //找到数据的最后一行
            int lastRowNum = sheetAt.getLastRowNum();
            //如果excel文件没有数据返回false
            if (0 == lastRowNum) {
                return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
            }
            //循环遍历导入数据库
            for (int i = 1; i <= lastRowNum; i++) {
                Row row = sheetAt.getRow(i);
                //找到第i行第1列的日期
                Date dateValue = row.getCell(0).getDateCellValue();
                //找到第i行第2列的number
                int numValue = (int) row.getCell(1).getNumericCellValue();
                //将日期类转为String
                String date2String = DateUtils.parseDate2String(dateValue);
                orderSettingService.uploadFile(date2String, numValue);
            }
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            log.error("文件导入异常e={}",e);
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }
}

